﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace 双色球案例
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //外层数组长度6, 最多能够存储6注
            //内层数组长度为8, 6红1蓝1倍数
            int[,] lottery = new int[6, 8];

            //添加时作为数组下标
            int count = 0;

            //创建随机类
            Random rnd = new Random();

            //开奖号码, 由6个红球1个蓝球组成
            int[] stNum = new int[7];
            bool isSt = false;
            bool isBuy = false;

            //展示菜单
            Console.WriteLine("--------------------------");
            Console.WriteLine("  欢迎使用控制台版双色球");
            Console.WriteLine("--------------------------");

            while(true)
            {
                Console.WriteLine("1. 购买彩票");
                Console.WriteLine("2. 查看已购彩票");
                Console.WriteLine("3. 开奖");
                Console.WriteLine("4. 查看中奖情况");
                Console.WriteLine("5. 退出系统");
                Console.WriteLine();
                //接收用户输入的编号
                string num = Console.ReadLine();
                switch (num)
                {
                    case "1":
                        if(!isSt)
                        {
                            Console.WriteLine("购买彩票");
                            //处理购买6个红球号码
                            #region
                            int redCount = 0;
                            while (true)
                            {
                                Console.WriteLine($"请输入你要选择的第{redCount + 1}个红球");
                                int redNum = Convert.ToInt32(Console.ReadLine());
                                //判断输入红球号码所属范围是否正确
                                if (redNum < 1 || redNum > 33)
                                {
                                    Console.ForegroundColor = ConsoleColor.Red;
                                    Console.WriteLine("\n输入的红球超出了范围(1-33)");
                                    Console.ResetColor();
                                    continue;
                                }

                                //判断是否输入重复的红球
                                bool isRedNumRepeat = false;
                                for (int i = 0; i < redCount; i++)
                                {
                                    if (lottery[count, i] == redNum)
                                    {
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.WriteLine("\n购买的红球重复!");
                                        Console.ResetColor();
                                        isRedNumRepeat = true;
                                        break;
                                    }
                                }
                                if (isRedNumRepeat)
                                {
                                    continue;
                                }

                                //将当前判断无误的红球放入到当前的彩票数组中
                                lottery[count, redCount] = redNum;
                                redCount++;//红球计数器+1

                                //购买了6个红球号码后结束循环
                                if (redCount == 6)
                                {
                                    break;
                                }
                            }
                            #endregion

                            //处理购买1个蓝球号码
                            #region
                            while (true)
                            {
                                Console.WriteLine($"请输入你要选择的蓝球");
                                int blueNum = Convert.ToInt32(Console.ReadLine());
                                //判断输入蓝球号码所属范围是否正确
                                if (blueNum < 1 || blueNum > 16)
                                {
                                    Console.ForegroundColor = ConsoleColor.Red;
                                    Console.WriteLine("\n输入的蓝球超出了范围(1-16)");
                                    Console.ResetColor();
                                    continue;
                                }

                                //将当前判断无误的蓝球放入到当前的彩票数组中
                                lottery[count, 6] = blueNum;
                                break;
                            }
                            #endregion

                            //处理购买的倍数
                            #region
                            while (true)
                            {
                                Console.WriteLine($"请输入你要选择的倍数");
                                int multiple = Convert.ToInt32(Console.ReadLine());
                                //判断输入倍数所属范围是否正确
                                if (multiple < 1 || multiple > 100)
                                {
                                    Console.ForegroundColor = ConsoleColor.Red;
                                    Console.WriteLine("\n输入的蓝球超出了范围(1-100)");
                                    Console.ResetColor();
                                    continue;
                                }

                                //将当前判断无误的倍数放入到当前的彩票数组中
                                lottery[count, 7] = multiple;
                                break;
                            }
                            #endregion

                            //将红球号码进行排序（蓝球号码，倍数号码不参与排序）
                            #region
                            //只有6个红球号码参与排序
                            //外层循环为 红球个数 - 1 轮
                            for (int i = 0; i < 5; i++)
                            {
                                for (int j = 0; j < 5 - i; j++)
                                {
                                    //升序排列
                                    if (lottery[count, j] > lottery[count, j + 1])
                                    {
                                        int tmp = lottery[i, j];
                                        lottery[i, j] = lottery[i, j + 1];
                                        lottery[i, j] = tmp;
                                    }
                                }
                            }
                            #endregion
                            Console.WriteLine();
                            //注数 + 1
                            count++;
                            isBuy = true;
                        }

                        break;
                    case "2":
                        //打印所购买的红球，蓝球和倍数
                        #region
                        for (int i = 0; i < count; i++)
                        {
                            Console.Write($"第{i + 1}注: ");
                            for (int j = 0; j < 8; j++ )
                            {
                                //红球
                                if(j < 6)
                                {
                                    Console.Write($"{lottery[i, j]} ");
                                }
                                //蓝球
                                else if(j == 6)
                                {
                                    Console.Write($"- {lottery[i, j]}");
                                }
                                //倍数
                                else
                                {
                                    Console.WriteLine($" *{lottery[i, j]}\t");
                                }
                            }
                            Console.WriteLine();
                        }
                        #endregion

                        break;
                    case "3":
                        //判断是否已经购买彩票
                        if(isBuy)
                        {
                            //判断是否已经开奖
                            if(isSt)
                            {
                                Console.WriteLine("已经开奖了");
                            }
                            else
                            {
                                Console.WriteLine("开奖");
                                //处理开奖的红球
                                #region
                                int redBallCount = 0;
                                while (true)
                                {
                                    int redBall = rnd.Next(1, 34);
                                    bool redBallRepeat = false;
                                    for (int i = 0; i < redBallCount; i++)
                                    {
                                        if (stNum[i] == redBall)
                                        {
                                            redBallRepeat = true;
                                            break;
                                        }
                                    }
                                    if (redBallRepeat)//随机红球号码出现重复
                                    {
                                        continue;
                                    }
                                    stNum[redBallCount] = redBall;
                                    redBallCount++;

                                    if(redBallCount == 6)
                                    {
                                        break;
                                    }
                                }
                                #endregion
                                //对开奖的红球进行排序
                                for (int i = 0; i < 5; i++)
                                {
                                    if (stNum[i] > stNum[i + 1])
                                    {
                                        int tmp = stNum[i];
                                        stNum[i] = tmp;
                                        stNum[i + 1] = tmp;
                                    }
                                }

                                //处理开奖的蓝球
                                stNum[6] = rnd.Next(1, 17);
                            }
                            isSt = true;
                        }
                        Console.WriteLine();

                        break;
                    case "4":
                        if(isSt)
                        {
                            int redWinCount = 0;
                            int blueWinCount = 0;
                            //判断中奖红球号码
                            #region
                            for (int i = 0; i < count; i++)
                            {
                                for (int j = 0; j < 6; j++)
                                {
                                    //lottery[i,j] 与 stNum[k] 相同即为中奖红球
                                    for (int k = 0; k < 6; k++)
                                    {
                                        if (lottery[i, j] == stNum[k])
                                        {
                                            redWinCount++;
                                        }
                                    }
                                }
                                #endregion
                                //判断中奖蓝球
                                //lottery[i,6] 与 stNum[6] 相同即为中奖蓝球
                                if (lottery[i, 6] == stNum[6])
                                {
                                    blueWinCount++;
                                }

                                //中奖注
                                #region
                                Console.WriteLine($"第{i + 1}注:");
                                if (redWinCount == 6 && blueWinCount == 1)
                                {
                                    Console.WriteLine($"恭喜您中的一等奖, 中奖金额为: 500万元 * {+lottery[i, 7]}");
                                }
                                else if (redWinCount == 6 && blueWinCount == 0)
                                {
                                    Console.WriteLine($"恭喜您中的二等奖, 中奖金额为: 5万元 * {+lottery[i, 7]}倍");
                                }
                                else if (redWinCount == 5 && blueWinCount == 1)
                                {
                                    Console.WriteLine($"恭喜您中的三等奖, 中奖金额为: 3000元 * {+lottery[i, 7]}倍");
                                }
                                else if ((redWinCount == 5 && blueWinCount == 0)
                                    || (redWinCount == 4 && blueWinCount == 1))
                                {
                                    Console.WriteLine($"恭喜您中的四等奖, 中奖金额为: 200元 * {+lottery[i, 7]}倍");
                                }
                                else if ((redWinCount == 4 && blueWinCount == 0)
                                    || (redWinCount == 3 && blueWinCount == 1))
                                {
                                    Console.WriteLine($"恭喜您中的五等奖, 中奖金额为: 10元 * {+lottery[i, 7]}倍");
                                }
                                else if ((redWinCount == 2 && blueWinCount == 1)
                                    || (redWinCount == 1 && blueWinCount == 1)
                                    || (redWinCount == 0 && blueWinCount == 1))
                                {
                                    Console.WriteLine($"恭喜您中的六等奖, 中奖金额为: 5元 * {+lottery[i, 7]}倍");
                                }
                                else
                                {
                                    Console.WriteLine("谢谢惠顾");
                                }
                                Console.WriteLine();
                                #endregion
                            }
                        }
                        else
                        {
                            Console.WriteLine("\n请先开奖");
                        }
                        Console.WriteLine();
                        break;
                    case "5":
                        Console.WriteLine("感谢您的使用，再见");
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("你的输入有误，请重新输入");
                        Console.ResetColor();
                        break;
                    
                }
            }
            

        }
    }
}